<template>
  <div class="container">
    <div class="containerselect">
      <div class="containerselects">
        <el-select v-model="schoolId"   clearable  @change="getEquipmentList" placeholder="请选择学校">
          <el-option
            v-for="item in schoolList"
            :key="item.shId"
            :label="item.shName"
            :value="item.shId"
          >
          </el-option>
        </el-select>
      </div>
      <div class="containerbotton">
        <el-button
          size="small"
          type="primary"
          @click="addActivationCode"
          style="margin-bottom: 20px; float: right"
          >添加空间
        </el-button>
      </div>
    </div>

    <el-table
      v-loading="isLoading"
      :data="tableData"
      style="width: 100%"
      :row-class-name="xxxTableRowClassName"
      :header-cell-style="{
        background: 'rgba(61,157,255,0.05)',
        color: '#333333',
      }"
    >
      <el-table-column
        prop="scenarioName"
        label="空间名称"
        width="width"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="scenarioSchoolName"
        label="所属学校"
        width="width"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="scenarioLocation"
        label="空间位置"
        width="width"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="scenarioRemark"
        label="备注"
        width="width"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="scenarioJudge"
        label="是否云电脑"
        width="width"
        align="center"
      >
      <template slot-scope="scope">
                  <el-switch
                    @change="changeIsExempt(scope.row)"
                    v-model="scope.row.scenarioJudge"
                    active-value="1"
                    :inactive-value="'0'"
                    active-color="#13ce66"
                  >
                  </el-switch>
                </template>

              </el-table-column>
   

      <el-table-column label="操作" width="width" align="center">
        <template slot-scope="{ row }">
          <el-button
            plain
            type="primary"
            size="small"
            @click="compile($event, row)"
            >修改</el-button
          >
          <el-button
            type="danger"
            size="small"
            plain
            @click="delActivationCode($event, row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :title="form.equipmentId ? '修改设备' : '添加设备'"
      :visible.sync="dialogFormVisible"
      width="50%"
      :close-on-click-modal="false"
    >
      <el-form
        label-position="left"
        :model="form"
        :rules="rules"
        ref="ruleForm"
        label-width="140px"
      >
        <el-form-item label="空间名称:" prop="scenarioName">
          <el-input
            style="width: 80%"
            v-model="form.scenarioName"
            placeholder="请输入空间名称名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="所属学校:" prop="scenarioSchoolId">
          <div style="display: flex">
            <el-select
              v-model="form.scenarioSchoolId"
              placeholder="学校"
              style="width: 80%"
            >
              <el-option
                v-for="item in schoolList"
                :key="item.shId"
                :label="item.shName"
                :value="item.shId"
              >
              </el-option>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item label="位置:" prop="scenarioLocation">
          <el-input
          v-model="form.scenarioLocation"
          style="width: 80%"
          placeholder="请输入位置"
          >
            
          </el-input>
        </el-form-item>
        <el-form-item label="备注:" prop="scenarioRemark">
          <el-input
            style="width: 80%"
            v-model="form.scenarioRemark"
            placeholder="请输入备注"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false"
          >取 消</el-button
        >
        <el-button
          :loading="disable"
          size="small"
          type="primary"
          @click="save()"
          >{{ disable ? "加载中" : "添 加" }}
        </el-button>
      </div>
    </el-dialog>

    <pagination-page @size="changeSize" @page="changePage" :total="total" />
  </div>
</template>

<script>
import {
  selectSchool,
  getEquipmentList,
  addEquipment,
  updateEquipment,
  deleteEquipment,
} from "@/api/device";
import {
  spaceList,
  addScenario,
  delScenario,
  updateCloudpc,
  schoolInfo
} from "@/api/spaceManagement";
export default {
  name: "",
  components: {},
  props: {},
  data() {
    return {
      schoolId: "",
      disable: false,
      total: -1,
      loading: false,
      page: {
        pageSize: 10,
        pageNum: 1,
      },
      dialogFormVisible: false,
      schoolList: [],
      tableData: [],
      scenarioLocationList: [
        {
          label: "大屏",
          value: "0",
        },
        {
          label: "云电脑",
          value: "1",
        },
        {
          label: "跑步设备",
          value: "2",
        },
        {
          label: "跳绳设备",
          value: "3",
        },
        {
          label: "引体向上设备",
          value: "4",
        },
        {
          label: "仰卧起坐",
          value: "5",
        },
      ],
      form: {
        scenarioSchoolId: "",
        scenarioRemark: "",
        scenarioLocation: "",
        equipmentDeploy: "",
      },
      rules: {
        scenarioName: [
          {
            required: true,
            message: "请输入名称",
          },
        ],
        scenarioSchoolId: [
          {
            required: true,
            message: "请选择学校",
          },
        ],
        scenarioRemark: [
          {
            required: true,
            message: "请输入备注",
          },
        ],

        scenarioLocation: [
          {
            required: true,
            message: "请选择位置",
          },
        ],
      },
      isLoading: false,
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getEquipmentList();
    this.getSelectSchool();
  },
  mounted() {},
  methods: {
    changeIsExempt(row) {
    
      updateCloudpc({
        scenarioId: row.scenarioId,
        scenarioJudge: row.scenarioJudge,
      }).then((response) => {
        if (response.code === 200) {
          this.getEquipmentList();
        }
      });
    },
    compile(evt, row) {
      let target = evt.target;
      if (target.nodeName == "SPAN") {
        target = evt.target.parentNode;
      }
      target.blur();
      this.dialogFormVisible = true;
      this.form = { ...row };
      this.$nextTick(() => {
        this.$refs.ruleForm.clearValidate();
      });
    },
    getEquipmentList() {
      let data = {
        ...this.page,
        scenarioSchoolId:this.schoolId
      };
      this.isLoading = true;
      spaceList(data).then((res) => {
        if (res.code == 200) {
          this.tableData = res.result;
          console.log(this.tableData, "1232232");
          this.total = res.total;
          console.log(this.total, '这是一个数据')
          this.isLoading = false;
        }
      });
    },
    getSelectSchool() {
      let data = {
        // userId: this.$store.state.user.USER_INFO.id,
      };
      schoolInfo(data).then((res) => {
        this.schoolList = res.result;
      });
    },
    addActivationCode() {
      this.dialogFormVisible = true;
      (this.form = {
        scenarioSchoolId: "",
        scenarioRemark: "",
        scenarioLocation: "",
        scenarioName:''
      }),
        this.$nextTick(() => {
          this.$refs.ruleForm.clearValidate();
        });
    },
    delActivationCode(evt, row) {
      let target = evt.target;
      this.$confirm("是否确定删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (target.nodeName == "SPAN") {
            target = evt.target.parentNode;
          }
          target.blur();

          let data = {
            scenarioId: row.scenarioId,
          };
          delScenario(data).then((res) => {
            if (res.code === 200) {
              this.getEquipmentList();
              this.$message.success("删除成功");
            } else {
              this.$message.error(res.message);
            }
          });
        })
        .catch(() => {
          if (target.nodeName == "SPAN") {
            target = evt.target.parentNode;
          }
          target.blur();

          this.$message({
            type: "info",
            message: "取消删除",
          });
        });
    },
    save() {
      let data = {
        ...this.form,
      };
      console.log(this.form)
      this.$refs["ruleForm"].validate(async (valid) => {
        if (valid) {
          this.disable = true;
          if (this.form.equipmentId) {
            addScenario(data).then((res) => {
              if (res.code === 200) {
                this.dialogFormVisible = false;
                (this.form = {
                  scenarioSchoolId: "",
                  scenarioRemark: "",
                  scenarioLocation: "",
                  scenarioName:'',
                }),
                  this.getEquipmentList();
                this.$message.success("保存成功");
                this.disable = false;
              } else {
                this.disable = false;
                this.dialogFormVisible = false;
                this.$message.error(res.message);
              }
            });
          } else {
            addScenario(data).then((res) => {
              if (res.code === 200) {
                this.dialogFormVisible = false;
                this.getEquipmentList();
                this.$message.success("保存成功");
                this.disable = false;
              } else {
                this.disable = false;
                this.dialogFormVisible = false;
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    xxxTableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 !== 0) {
        return "statistics-warning-row";
      } else {
        return "";
      }
    },
    changeSize(size) {
      this.page.pageSize = size;
      this.getEquipmentList();
    },
    changePage(page) {
      this.page.pageIndex = page;
      this.getEquipmentList();
    },
  },
};
</script>

<style scoped lang="scss">
::v-deep .el-table__row.statistics-warning-row {
  background: rgba(61, 157, 255, 0.05);
}
::v-deep .el-upload-list__item {
  display: none;
}
::v-deep .el-form-item__label {
  padding-left: 40px;
}
.container {
  .containerselect {
    display: flex;
    justify-content: space-between;
    .containerselects {
    }
    .containerbotton {
      margin-top: 0.5rem;
    }
  }
}
</style>
